<?php
/**
 * paypal 登录回调功能
 * http://18.163.191.164/paypal-hook/returnurl.php
 */
/**
 * http://demo/paypal-hook/returnurl.php?code=C21AAJPFkKNoZf8eT6vrWk_2GD7yIiU_3Hf14BHDLEYm9V34rEqW53qDuPWmQNWh_AD5Gcni97R5312AIEpuTMHNj7OWiH2bw&scope=openid%20profile%20email
 */


use PayPalHttp\HttpClient;
use PayPalHttp\HttpRequest;
use PaypalPayoutsSDK\Core\ProductionEnvironment;
use PaypalPayoutsSDK\Core\SandboxEnvironment;

require __DIR__  . '/vendor/autoload.php';

error_reporting(E_ALL);

// 回调类型
function sendBack($data, $msg='', $code=200){
    $backinfo = json_encode([
        'code'=> $code,
        'msg' => $msg,
        'data'=> $data,
    ]);
    echo $backinfo;
    exit();
}

// Creating an environment
$clientId = "AVQ68-wx9VQrSNHfJTg2PWTWr37_6-nE-XRQV6pf2wro3ZUG70Wt29qaeQHKhGldyE55AsGgRZykzNQN";
$clientSecret = "EPnwkDY6xOUDCRJP0dBA9KeB8ckxAndYmE7djY7ZpUbistKPaCFQZV7HLDGqlIK7WZF1SK0CcXYYdc2l";
define('SANDBOX', true);


if(SANDBOX){
    $environment = new SandboxEnvironment($clientId, $clientSecret);
} else {
    $environment = new ProductionEnvironment($clientId, $clientSecret);
}


if(isset($_GET['code'])) {
    $code = trim($_GET['code']);
    $scope = trim($_GET['scope']);
    // header();
    // $request = new HttpRequest("/v1/oauth2/token", "POST");
    // $request->headers = [
    //     "Content-Type" => "application/json",
    //     "Authorization" => "Basic " . $environment->authorizationString(),
    //     "user-agent" => $_SERVER['HTTP_USER_AGENT'],
    // ];
    // $request->body = [
    //     "grant_type" => "authorization_code",
    //     "code" => $code,
    // ];
    // $client = new HttpClient($environment);
    // $res = $client->execute($request);

    include_once './PYLogin.php';

    $PYLogin = new PYLogin($clientId, $clientSecret, true);
    $PYLogin->headers = [
        "Content-Type" => "application/json",
        "Authorization" => "Basic " . $PYLogin->authorizationString(),
    ];
    $PYLogin->body = [
        "grant_type" => "authorization_code",
        "code" => $code,
    ];
    $PYLogin->getToken();

$a = fopen('c.txt', 'a+');
fwrite($a, var_export($res, true) );
fclose($a);

}


/**
 * 
 * 
curl -X POST https://api-m.sandbox.paypal.com/v1/oauth2/token \
-H 'Authorization: Basic QVZRNjgtd3g5VlFyU05IZkpUZzJQV1RXcjM3XzYtbkUtWFJRVjZwZjJ3cm8zWlVHNzBXdDI5cWFlUUhLaEdsZHlFNTVBc0dnUlp5a3pOUU46RVBud2tEWTZ4T1VEQ1JKUDBkQkE5S2VCOGNreEFuZFltRTdkalk3WnBVYmlzdEtQYUNGUVpWN0hMREdxbElLN1daRjFTSzBDY1hZWWRjMmw=' \
-d 'grant_type=authorization_code&code=C21AAIZf1s2lpQ0ZOvAQ-y4yrmWp581AI-FS-cAzkDJ8dz6aMtexl61DsPmeRcl0MTW81mmRhqQu2VpgUxsA_tPF-vto8M3-w'
 * 
 * 
 */



